<!DOCTYPE html>
<html>
<head>
    <title>预览报告</title>
    <#include "../../include/head-file.ftl">
    <script src="${base}/static/plugins/html2pdf/html2pdf.bundle.min.js" defer></script>
    <script src="${base}/static/plugins/html2docx/html-docx.js" defer></script>
    <script src="${base}/static/plugins/file-saver/Blob.js" defer></script>
    <script src="${base}/static/plugins/file-saver/FileSaver.js" defer></script>
</head>
<body style="background: white;padding: 0 120px 20px;overflow-y:auto;height: calc(100vh - 20px)">
<button type="button" onclick="exportDoc()" class="custom-bottom">导出</button>
<button type="button" onclick="exportDocx()" class="custom-bottom">导出Word</button>
</body>
</html>
<script>
    var id = ms.util.getParameter("id");
    var contentTitle = ms.util.getParameter("contentTitle");
    var contentAuthor = ms.util.getParameter("contentAuthor");
    var contentDatetime = ms.util.getParameter("contentDatetime");
    var exportDataElem = document.getElementById("export-data");
    document.write('<div id="export-data"><head><meta charset="UTF-8"></head><div style="text-align: center;"> <p><h1>'+contentTitle+'</h1></p><p style="font-size: 18px;">作者：'+contentAuthor+'&nbsp;&nbsp;&nbsp;&nbsp;发布时间：'+contentDatetime+'</p></div>' + localStorage.getItem('htmlDetail'+id) + '</div>')
    localStorage.removeItem('htmlDetail'+id)
    function exportDoc(){
        var contentElem = document.getElementById("export-data");
        var pdfOpt = {
            margin:       100,
            filename:     contentTitle + ".pdf",
            jsPDF:        { unit: 'px', format: [1240, 1754], orientation: 'portrait' }
        }
        var worker = html2pdf().set(pdfOpt).from(contentElem).save();
    }
    function exportDocx(){
        var contentElem = document.getElementById("export-data");
        convertChartsToBase64(contentElem)
        var converted = htmlDocx.asBlob(contentElem.innerHTML, {orientation: 'portrait', margins: {top: 720}});
        saveAs(converted, contentTitle + ".docx");
    }
    function convertChartsToBase64(contentDocument) {
        // 找到所有的图表 （echart）
        let canvases = contentDocument.querySelectorAll("canvas");
        // var reportchartsContainerArr = document.getElementsByClassName('report-charts-container')
        // 遍历图表，转换为 base64 静态图片
        canvases.forEach((canvas, i) => {
            var url = canvas.toDataURL('image/jpg')
            let img = document.createElement("img");
            img.src = url;
            img.width='600'
            img.height='300'
            canvas.parentElement.replaceChild(img, canvas);
            // reportchartsContainerArr[i].appendChild(img)
            // canvas.parentNode.replaceChild(img, canvas);
        });
    }
    function downLoadMix(res, fileName){
        var blob = new Blob([res]);
        if ("msSaveOrOpenBlob" in navigator) {
            window.navigator.msSaveOrOpenBlob(blob, fileName);
        } else {
            var link = document.createElement('a')
            var body = document.querySelector('body')
            link.href = window.URL.createObjectURL(blob)
            link.download = fileName
            link.style.display = 'none'
            body.appendChild(link)
            link.click()
            body.removeChild(link)
            window.URL.revokeObjectURL(link.href)
        }
    }

</script>
<style>
    .custom-bottom{
        color: #fff;
        background-color: #409eff;
        border-color: #409eff;
        display: inline-block;
        line-height: 1;
        white-space: nowrap;
        cursor: pointer;
        border: 1px solid #dcdfe6;
        -webkit-appearance: none;
        text-align: center;
        box-sizing: border-box;
        outline: none;
        margin: 0;
        transition: .1s;
        font-weight: 500;
        -moz-user-select: none;
        -webkit-user-select: none;
        -ms-user-select: none;
        padding: 12px 20px;
        font-size: 14px;
        border-radius: 4px;
        margin-top: 20px;
    }

    .custom-bottom:focus, .custom-bottom:hover {
        background: #66b1ff;
        border-color: #66b1ff;
        color: #fff;
    }
</style>
